package com.tusuo.stds.ui.rank;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.RankList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import java.util.ArrayList;
import java.util.List;

import cn.carbs.android.avatarimageview.library.AvatarImageView;
import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_rank)
public class RankActivity extends BaseToolbarActivity<RankView, RankPresenter>
                                implements RankView {

    public static final int RANK_TYPE_SCENE = 1;
    public static final int RANK_TYPE_MODULE = 2;
    public static final int RANK_TYPE_LEVEL = 3;

    @ViewById(R.id.rank_aiv_avatar)
    AvatarImageView aiv;
    @ViewById(R.id.rank_tv_name)
    TextView tvName;
    @ViewById(R.id.rank_tv_school_name)
    TextView tvSchoolName;
    @ViewById(R.id.rank_tv_score)
    TextView tvScore;
    @ViewById(R.id.rank_tv_rank)
    TextView tvRank;
    @ViewById(R.id.rank_tv_title)
    TextView tvTitle;

    @ViewById(R.id.rank_layout_no_list)
    LinearLayout layoutNoList;
    @ViewById(R.id.list_rank)
    ListView listView;

    @StringRes(R.string.toolbar_title_rank)
    String toolbarTitle;

    @ColorRes(R.color.white)
    int colorWhite;

    private int mType;
    private int mId;

    @AfterInject
    void init() {
        mType = getIntent().getIntExtra("type", 0);
        mId = getIntent().getIntExtra("id", 0);
    }

    @AfterViews
    void initData() {
        setupUI(this);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public RankPresenter createPresenter() {
        return new RankPresenter(this, mType, mId);
    }

    @Override
    public void setRankList(RankList list) {
        if (TextUtils.isEmpty(list.MyRank.Photo)) {
            aiv.setImageResource(R.drawable.img_avatar_default);
        } else {
            Glide
                    .with(this)
                    .load(Utils.getCorrectImageUrl(list.MyRank.Photo))
                    .centerCrop()
                    .crossFade()
                    .into(aiv);
        }

        tvName.setText(Utils.Text.getTruncatedString(list.MyRank.Name, 16));

        if (TextUtils.isEmpty(list.MyRank.SchoolName)) {
            tvSchoolName.setText("神秘学校");
        }else {
            tvSchoolName.setText(list.MyRank.SchoolName);
        }

        tvScore.setText(String.valueOf(list.MyRank.Score));

        if (list.MyRank.Rank < 1) {
            tvRank.setText("暂无排名信息");
        } else if (list.MyRank.Rank > 500) {
            tvRank.setText("第500+名");
        }else {
            tvRank.setText("第" + getString(R.string.rank_my_rank, list.MyRank.Rank));
        }

        tvTitle.setText(list.Title);

        if (list.RankList.size() > 0) {
            listView.setAdapter(new RankListAdapter(this, list.RankList));
            layoutNoList.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
        } else {
            layoutNoList.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }
    }
}
